<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
session_start();

class User extends CI_Controller {

	function index() {
		$this -> load -> model('user_model');
		$this -> register();
	}

	function login() {
		// user login 

		// form validation
		$this -> load -> library('form_validation');
		$this -> form_validation -> set_rules('username', 'Username', 'trim|required|max_length[20]|alpha_numeric|xss_clean');
		$this -> form_validation -> set_rules('password', 'Password', 'trim|required|max_length[20]|alpha_numeric|xss_clean');

		if ($this -> form_validation -> run() == FALSE) {
			//validation error
			$data['loginerror'] = (validation_errors());

			$this -> load -> view('view_login', $data);
		} else {
			//to database
			$this -> load -> model('user_model');
			$username = $this -> input -> post('username');
			$password = $this -> input -> post('password');
			$check = $this -> user_model -> check_login($username, $password);

			if ($check == 0) {
				// user valid to login
				$_SESSION['userObject'] = $this -> user_model -> getUserByUsername($username);
				$_SESSION['username'] = $_POST['username'];
				
				redirect('mainpage');
			} 
			 elseif ($check == 1) {
				// incorrect username or password
				$data['notification'] = "Please check your username or password";
				$this -> load -> view('view_login', $data);
			}
		}
	}

	function register() {
		//sign up
		
		//form validation
		$this -> load -> library('form_validation');

		$this -> form_validation -> set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[20]|alpha_numeric|xss_clean|callback_username_notexist');
		$this -> form_validation -> set_rules('fname', 'First Name', 'trim|required|max_length[55]|xss_clean');
		$this -> form_validation -> set_rules('lname', 'Last Name', 'trim|required|max_length[55]|xss_clean');
		$this -> form_validation -> set_rules('email', 'Email', 'trim|required|xss_clean|max_length[45]|valid_email|callback_email_notexist');
		$this -> form_validation -> set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[20]|alpha_numeric|xss_clean');
		$this -> form_validation -> set_rules('password_conf', 'Confirmation Password', 'trim|required|min_length[6]|alpha_numeric|matches[password]|xss_clean');

		if ($this -> form_validation -> run() == FALSE) {
			//validation error
			$data['registererror'] = (validation_errors());
			$this -> load -> view('view_register', $data);
		} else {
			//to database
			

			$this -> load -> model('user_model');
			$username = $this -> input -> post('username');
			$fname = $this -> input -> post('fname');
			$lname = $this -> input -> post('lname');
			$password = $this -> input -> post('password');
			$email = $this -> input -> post('email');
			
			//$activation_code = $this -> _random_string(10);

			$this -> user_model -> register_user($username, $password, $fname, $lname, $email);

			
			$data['success'] = "success register";
			$this -> load -> view('view_main', $data);
			
			}

	}

	

	function username_notexist($username) {
		// check username already used or not
		$this -> load -> model('user_model');
		$this -> form_validation -> set_message('username_notexist', 'username already used');
		if ($this -> user_model -> check_username($username)) {
			return false;
		} else {
			return true;
		}

	}

	function email_notexist($email) {
		//check email already used or not
		$this -> load -> model('user_model');
		$this -> form_validation -> set_message('email_notexist', 'email already used');
		if ($this -> user_model -> check_email($email)) {
			return false;
		} else {
			return true;
		}

	}

	/*
	 * Function to destroy session and user logout
	 */ 
	function logout() {
		//$this->session->set_userdata('logged_in', FALSE);
		unset($_SESSION['username']);
		unset($_SESSION['userObject']);
		redirect('mainpage');
	}





}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
